package main

import (
	"encoding/json"
	"fmt"
	"traning/nacos_test/config"

	"github.com/nacos-group/nacos-sdk-go/clients"
	"github.com/nacos-group/nacos-sdk-go/common/constant"
	"github.com/nacos-group/nacos-sdk-go/vo"
)

func main() {
	// 创建clientConfig
	cc := constant.ClientConfig{
		NamespaceId:         "8e2bd92e-b54e-4a77-9197-e93a95df73f9", // 如果需要支持多namespace，我们可以场景多个client,它们有不同的NamespaceId。当namespace是public时，此处填空字符串。
		TimeoutMs:           5000,
		NotLoadCacheAtStart: true,
		LogDir:              "tmp/nacos/log",
		CacheDir:            "tmp/nacos/cache",
		RotateTime:          "1h",
		MaxAge:              3,
		LogLevel:            "debug",
	}

	// 至少一个ServerConfig
	sc := []constant.ServerConfig{
		{
			IpAddr:      "www.gfam.top",
			Port:        8848,
		},
	}

	// 创建动态配置客户端的另一种方式 (推荐)
	configClient, err := clients.CreateConfigClient(map[string]interface{}{
		"serverConfigs": sc,
		"clientConfig":  cc,
	})
	if err != nil {
		panic(err)
	}

	content, err := configClient.GetConfig(vo.ConfigParam{
		DataId: "user-web.json",
		Group:  "dev"})
	fmt.Println(content)

	srvConfig := config.ServerConfig{}
	err = json.Unmarshal([]byte(content), &srvConfig)
	if err != nil {
		panic(err)
	}
	//err = configClient.ListenConfig(vo.ConfigParam{
	//	DataId: "user-web.yaml",
	//	Group:  "dev",
	//	OnChange: func(namespace, group, dataId, data string) {
	//		fmt.Println("配置发生变化")
	//		fmt.Println("group:" + group + ", dataId:" + dataId + ", data:" + data)
	//	},
	//})
	//if err != nil {
	//	panic(err)
	//}
	//time.Sleep(300* time.Second)
}
